﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using RAE.Logica.Controladores;
using RAE.Logica.Info;
using RAE.GUI.Inicio;
using System.Text.RegularExpressions;

namespace RAE.GUI.Info

{
    /// <summary>
    /// Interaction logic for Registro.xaml
    /// </summary>
    public partial class DialogoRegistro : Window
    {
        private byte[] fotografia;
        private VentanaPrincipal vp;

        public DialogoRegistro(VentanaPrincipal vp)
        {
            InitializeComponent();
            fotografia = null;
            combo_pais.ItemsSource = Aplicacion.paises.Values;
            this.vp = vp;
        }

        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            //dlg.FileName = "Document"; // Default file name
            //dlg.DefaultExt = ".jpg"; // Default file extension
            dlg.Filter = "Fotografía (.jpg)|*.jpg|Cualquier archivo|*.*"; // Filter files by extension
            dlg.CheckFileExists = true;
            dlg.DereferenceLinks = true;

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string selectedFileName = dlg.FileName;
                fotografia = File.ReadAllBytes(selectedFileName);
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri(selectedFileName);
                bitmap.EndInit();
                image_foto.Source = bitmap;
            }
        }

        private void RegistrarClick(object sender, RoutedEventArgs e)
        {
            Ajedrecista a = new Ajedrecista();
            a.cedula = text_cedula.Text;
            a.nombre= text_nombre.Text;
            a.apellidos = text_apellidos.Text;
            a.fechaNacimiento = date_fecha_nacimiento.SelectedDate;
            a.nacionalidad = (Pais) combo_pais.SelectedValue;
            a.esResidente = check_residente.IsChecked ?? false;
            a.tel_residencia = text_residencia.Text;
            a.tel_celular = text_celular.Text;
            a.email = text_email.Text;
            a.comentario = text_comentario.Text;
            a.fotografia = this.fotografia;

            Aplicacion.registrarAjedrecista(a);
            vp.setGrid();
            this.Close();
        }

        private void CancelarClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void QuitarFoto(object sender, RoutedEventArgs e)
        {
            image_foto.Source = null;
            fotografia = null;
        }

        private void text_cedula_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            try
            {
                Regex regex = new Regex("^(-)?\\d+$|^-?$");
                e.Handled = !(regex.IsMatch(e.Text));
            }
            catch (Exception f)
            { }
        }

        private void text_nombre_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            try
            {
                Regex regex = new Regex("[a-zA-Z]+$");
                e.Handled = !(regex.IsMatch(e.Text));
            }
            catch (Exception f)
            { }
        }


    }
}
